from analyst_agent import AnalystAgent
from purpose_agent import PurposeAgent
from mcp_server_graph.utils.graph_service import GraphService
from mcp_server_graph.utils.data_handler import get_analyst_agent_input

id2purpose = {
    0: "人车家",
    1: "异网用户发展",
    2: "群体运营",
    3: "诈骗识别",
    4: "集团产品推荐",
    5: "其他"
}


def run(usr_input, ids):
    purpose_agent = PurposeAgent()
    analyst_agent = AnalystAgent()
    graph_service = GraphService()

    data = purpose_agent.purpose_chat(usr_input)
    purpose = data['purpose']  
    reason = data['reason']  
    
    print(f"识别到的意图：{id2purpose[purpose]}，理由：{reason}")

    summary = ""
    if purpose == 0:
        vertex_set, edge_set, extra_info = graph_service.get_user_car_family(users=ids)
        graph_service.simplify_for_explain(vertex_set, edge_set)
        summary = analyst_agent.analyst_chat(get_analyst_agent_input(usr_input, (vertex_set, edge_set), purpose, extra_info))
    elif purpose == 1:
        vertex_set, edge_set, extra_info = graph_service.get_new_customer_recommendation(users=ids)
        graph_service.simplify_for_explain(vertex_set, edge_set)
        summary = analyst_agent.analyst_chat(get_analyst_agent_input(usr_input, (vertex_set, edge_set), purpose, extra_info))
    elif purpose == 2:
        vertex_set, edge_set, extra_info = graph_service.get_current_customer_recommendation(users=ids)
        graph_service.simplify_for_explain(vertex_set, edge_set)
        summary = analyst_agent.analyst_chat(get_analyst_agent_input(usr_input, (vertex_set, edge_set), purpose, extra_info))
    elif purpose == 3:
        vertex_set, edge_set, extra_info = graph_service.get_fraud_meta(users=ids)
        summary = analyst_agent.analyst_chat(get_analyst_agent_input(usr_input, (vertex_set, edge_set), purpose, extra_info))
    elif purpose == 4:
        vertex_set, edge_set, extra_info = graph_service.get_group_product_recommendation(users=ids)
        graph_service.simplify_for_explain(vertex_set, edge_set)
        summary = analyst_agent.analyst_chat(get_analyst_agent_input(usr_input, (vertex_set, edge_set), purpose, extra_info))
    elif purpose == 5:
        data = graph_service.get_2_step_neighbour_meta(users=ids)
        graph_service.simplify_for_explain(*data)
        summary = analyst_agent.analyst_chat(get_analyst_agent_input(usr_input, data, purpose))
    else:
        return "意图识别错误"

    return summary

def test_user_car_family():
    # U00001为负样本，U02369为正样本
    ids = ['U02369','U00001']
    usr_input = "帮我找出北京地区经常去4S店，可能有购车意向的用户"
    result = run(usr_input, ids)
    print(result) #根据用户问题，找出了正样本进行分析推荐，不分析负样本（无购车意向）

def test_new_customer_recommendation():
    # 'U02925'是北大的用户
    ids = ['U02925']
    usr_input = "基于海淀区校园用户的特点，进行异网用户拉新"
    result = run(usr_input, ids)
    print(result) #找出了北大的LAC圈，并分析了圈内的异网用户和用户特征，给出了营销建议

def test_current_customer_recommendation():
    # 'U02899' 正样本：常驻华为北研所，负样本：'U00874'，常驻建行
    ids = ['U02899', 'U00874']
    usr_input = "日间常驻海淀区科技公司附近的用户群体中，哪些人资费明显低于其他人，请给出营销建议"
    result = run(usr_input, ids)
    print(result) #正负样本都会分析，不传图谱信息的情况下会把离群用户当作低消用户

def test_fraud_detection():
    # 'U00104','U00039'两个都是诈骗用户
    ids = ['U00026']
    usr_input = "帮我筛查最近一周有大量短时长国际通话的新入网用户，识别诈骗风险"
    result = run(usr_input, ids)
    print(result)

def test_group_product_recommendation():
    # 'U00109'是国家电网员工，订购了移动看家、移动高清，国家电网尚未订购
    # 没查到数据
    ids = ['U01118', 'U01117']
    usr_input = "查询集团用户群体中个人订购较多的产品，并发掘产品的潜在合作企业，制定推荐策略"
    result = run(usr_input, ids)
    print(result) #识别到了用户'U01118'为国电员工，能够推荐国电推荐产品

def test_two_step_neighbour():
    ids = ['U02369','U02369','U00069']
    usr_input = "查询用户U02369的二度关系网络，识别高价值用户，并给出营销策略"
    result = run(usr_input, ids)
    print(result) #取出了用户二度关系，并解读分析



if __name__=='__main__':
    # test_user_car_family() #✅
    # test_new_customer_recommendation()#✅
    # test_current_customer_recommendation() #✅
    test_fraud_detection() 
    # test_group_product_recommendation() #✅
    # test_two_step_neighbour() #✅
    ...